$(function() {
	$.ajax({
		"type": "get",
		"url": "http://114.215.175.223:8080/xm-manager/count/init",
		"success": function(data) {
			console.log(data.data);
			var userCount = data.data.userCount;
			var orderCount = data.data.orderCount;
			var broadcastCount = data.data.broadcastCount;
			var onLineCount = data.data.onLineCount;
			$(".userCount").html(userCount);
			$(".orderCount").html(orderCount);
			$(".broadcastCount").html(broadcastCount);
			$(".onLineCount").html(onLineCount);
		}
	});

	$.ajax({
		url: 'http://114.215.175.223:8080/xm-manager/get/quantity_count',
		type: 'post',
		data: {
			startTime: "2017-2-19",
			endTime: endTime
		},
		dataType: 'json',
		success: function(data) {
			var arrTime = [];
			var arrTotal = [];
//			console.log(data);
//			compeleteData(data);
			var time = compeleteData(data);
			console.log(time);
			for (var i = 0; i < time.data.length; i++) {
				arrTime.push(time.data[i].date);
				arrTotal.push(time.data[i].total);
			}
			
			$.ajax({
				type:"post",
				url:"http://114.215.175.223:8080/xm-manager/get/complete_count",
				data: {
					startTime: "2017-2-19",
					endTime: "2017-3-3"
				},
				dataType: 'json',
				success:function(dayDate){
//					console.log(dayDate);
//					console.log(dayDate.data[0].dayTotal);
					compeletedayDate(dayDate);
					var time2 = compeletedayDate(dayDate);
//					console.log(time2);
//					console.log(time2.data[0].dayTotal);
					var daytotal = [];
					for(var j = 0;j<time2.data.length;j++){
						daytotal.push(time2.data[j].dayTotal);
					}
//					console.log(daytotal);
					
					option1 = {
						title: {
							text: '下单量&乘单量'
						},
						tooltip: {
							trigger: 'axis'
						},
						toolbox: {
							show: true,
							feature: {
								dataZoom: {
									yAxisIndex: 'none'
								},
								dataView: {
									readOnly: false
								},
								magicType: {
									type: ['line', 'bar']
								},
								restore: {},
								saveAsImage: {}
							}
						},
						xAxis: {
							type: 'category',
							boundaryGap: false,
							data: arrTime
						},
						yAxis: {
							type: 'value',
							axisLabel: {
								formatter: '{value} 人'
							}
						},
						series: [{
							name: '下单量',
							type: 'line',
							data: arrTotal,
							markPoint: {
								data: [{
									type: 'max',
									name: '日下单量'
								}, {
									type: 'min',
									name: '日成单量'
								}]
							},
							markLine: {
								data: [{
									type: 'average',
									name: '平均值'
								}]
							}
						}, {
							name: '乘单量',
							type: 'line',
							data: daytotal,
							markPoint: {
								data: [{
									name: '周最低',
									value: -2,
									xAxis: 1,
									yAxis: -1.5
								}]
							},
							markLine: {
								data: [{
										type: 'average',
										name: '平均值'
									},
									[{
										symbol: 'none',
										x: '90%',
										yAxis: 'max'
									}, {
										symbol: 'circle',
										label: {
											normal: {
												position: 'start',
												formatter: '最大值'
											}
										},
										type: 'max',
										name: '最高点'
									}]
								]
							}
						}],
						dataZoom: [{
							type: 'slider',
							xAxisIndex: 0,
							start: 10,
							end: 60
						}, {
							type: 'inside',
							xAxisIndex: 0,
							start: 10,
							end: 60
						}, {
							type: 'slider',
							yAxisIndex: 0,
							start: 30,
							end: 80
						}, {
							type: 'inside',
							yAxisIndex: 0,
							start: 30,
							end: 80
						}],
					};
					myChart1.setOption(option1);
				}
			});
		}
	});
	
	$.ajax({
		"type":"post",
		"url":"http://114.215.175.223:8080/xm-manager/get/user",
		data: {
			startTime: "2017-2-19",
			endTime: endTime,
			sex:"男"
		},
		dataType: 'json',
		success:function(data){
			console.log(data);
		}
	})
	
	function add(time1, time2) {
		var data1 = new Date(time1).getTime();
		var data2 = new Date(time2).getTime();
		var flag = Number(data1) - Number(data2);
		return flag;
	}

	function getNowFormatDate() {
		var date = new Date();
		var seperator1 = "-";
		var seperator2 = ":";
		var month = date.getMonth() + 1;
		var strDate = date.getDate();
		if (month >= 1 && month <= 9) {
			month = "0" + month;
		}
		if (strDate >= 0 && strDate <= 9) {
			strDate = "0" + strDate;
		}
		var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate;
		return currentdate;
	}

	var endTime = getNowFormatDate();

	function p(s) {
		s = Number(s);
		return s < 10 ? '0' + s : s;
	}

	function addZero(str) {
//		console.log(str);
		var arr = str.split("-");
		for (var i = 0; i < arr.length; i++) {
			arr[i] = p(arr[i]);
		}
		return arr.join("-");
		console.log(str);
	}

	var dayseconds = add("2017-02-24", "2017-02-23");
	console.log(dayseconds);

	function count(time1, time2) {
		var data1 = new Date(time1).getTime();
		var data2 = new Date(time2).getTime();
		var data3 = data2;
		var counts = [];
		while (data3 >= data1) {
			data3 -= dayseconds;
			var day = new Date(data3);
			counts.push(day.getFullYear() + "-" + (day.getMonth() + 1) + "-" + (day.getDate() + 1));
		}
		return counts.reverse();
	}

	function compeleteData(data) {
		for (var i = 0; i < data.data.length; i++) {
			data.data[i]["date"] = addZero(data.data[i]["date"]);
			if (i < (data.data.length - 1) && add(data.data[i + 1]["date"], data.data[i]["date"]) > dayseconds) {
				var arr = count(data.data[i]["date"], data.data[i + 1]["date"]);
				for (var j = 1; j < arr.length - 1; j++) {
					arr[j] = addZero(arr[j]);
					var obj = {
						"total": 0
					};
					obj["date"] = arr[j];
					data.data.splice(i + 1, 0, obj);
					i++;
				}
			}
		}
		return data;
	}
	
	function compeletedayDate(data) {
		for (var i = 0; i < data.data.length; i++) {
			data.data[i]["dayDate"] = addZero(data.data[i]["dayDate"]);
			if (i < (data.data.length - 1) && add(data.data[i + 1]["dayDate"], data.data[i]["dayDate"]) > dayseconds) {
				var arr = count(data.data[i]["dayDate"], data.data[i + 1]["dayDate"]);
				for (var j = 1; j < arr.length - 1; j++) {
					arr[j] = addZero(arr[j]);
					var obj = {
						"dayTotal":0
					};
					obj["dayDate"] = arr[j];
					data.data.splice(i + 1, 0, obj);
					i++;
				}
			}
		}
		return data;
	}
	
	
});